<template>
  <div class="register-page">
    <div class="register-header">
      <h2 class="register-title">加入零码智绘</h2>
      <p class="register-subtitle">开启您的智能编程之旅</p>
    </div>

    <a-form
      :model="formState"
      name="basic"
      autocomplete="off"
      @finish="handleSubmit"
      class="register-form"
      layout="vertical"
    >
      <a-form-item
        label="账号"
        name="userAccount"
        :rules="[{ required: true, message: '请输入账号' }]"
      >
        <a-input
          v-model:value="formState.userAccount"
          placeholder="请输入您的账号"
          size="large"
          class="custom-input"
        >
          <template #prefix>
            <UserOutlined class="input-icon" />
          </template>
        </a-input>
      </a-form-item>

      <a-form-item
        label="密码"
        name="userPassword"
        :rules="[
          { required: true, message: '请输入密码' },
          { min: 8, message: '密码不能小于 8 位' },
        ]"
      >
        <a-input-password
          v-model:value="formState.userPassword"
          placeholder="请输入您的密码"
          size="large"
          class="custom-input"
        >
          <template #prefix>
            <LockOutlined class="input-icon" />
          </template>
        </a-input-password>
      </a-form-item>

      <a-form-item
        label="确认密码"
        name="checkPassword"
        :rules="[
          { required: true, message: '请确认密码' },
          { min: 8, message: '密码不能小于 8 位' },
          { validator: validateCheckPassword },
        ]"
      >
        <a-input-password
          v-model:value="formState.checkPassword"
          placeholder="请再次输入密码"
          size="large"
          class="custom-input"
        >
          <template #prefix>
            <SafetyOutlined class="input-icon" />
          </template>
        </a-input-password>
      </a-form-item>

      <div class="password-strength">
        <div class="strength-text">密码强度：</div>
        <div class="strength-bar">
          <div
            class="strength-fill"
            :class="passwordStrengthClass"
            :style="{ width: passwordStrengthWidth }"
          ></div>
        </div>
        <span class="strength-label" :class="passwordStrengthClass">
          {{ passwordStrengthText }}
        </span>
      </div>

      <a-form-item>
        <a-button
          type="primary"
          html-type="submit"
          size="large"
          class="register-button"
          :loading="loading"
        >
          <span v-if="!loading">立即注册</span>
          <span v-else>注册中...</span>
        </a-button>
      </a-form-item>
    </a-form>

    <div class="register-footer">
      <div class="login-tip">
        已有账号？
        <RouterLink to="/user/login" class="login-link"> 立即登录 </RouterLink>
      </div>

      <div class="agreement">
        <a-checkbox v-model:checked="agreeToTerms" class="agreement-checkbox">
          我已阅读并同意
        </a-checkbox>
        <a href="#" class="agreement-link">《用户协议》</a>
        和
        <a href="#" class="agreement-link">《隐私政策》</a>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
import { userRegister } from '@/api/userController.ts'
import { message } from 'ant-design-vue'
import { reactive, ref, computed } from 'vue'
import { UserOutlined, LockOutlined, SafetyOutlined } from '@ant-design/icons-vue'

const router = useRouter()
const loading = ref(false)
const agreeToTerms = ref(true)

const formState = reactive<API.UserRegisterRequest>({
  userAccount: '',
  userPassword: '',
  checkPassword: '',
})

/**
 * 计算密码强度
 */
const passwordStrength = computed(() => {
  const password = formState.userPassword
  if (!password) return 0

  let strength = 0
  // 长度检查
  if (password.length >= 8) strength += 1
  // 包含数字
  if (/\d/.test(password)) strength += 1
  // 包含小写字母
  if (/[a-z]/.test(password)) strength += 1
  // 包含大写字母
  if (/[A-Z]/.test(password)) strength += 1
  // 包含特殊字符
  if (/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password)) strength += 1

  return strength
})

const passwordStrengthWidth = computed(() => {
  return `${(passwordStrength.value / 5) * 100}%`
})

const passwordStrengthClass = computed(() => {
  if (passwordStrength.value <= 1) return 'weak'
  if (passwordStrength.value <= 3) return 'medium'
  return 'strong'
})

const passwordStrengthText = computed(() => {
  if (passwordStrength.value <= 1) return '弱'
  if (passwordStrength.value <= 3) return '中等'
  return '强'
})

/**
 * 验证确认密码
 * @param rule
 * @param value
 * @param callback
 */
const validateCheckPassword = (rule: unknown, value: string, callback: (error?: Error) => void) => {
  if (value && value !== formState.userPassword) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

/**
 * 提交表单
 * @param values
 */
const handleSubmit = async (values: API.UserRegisterRequest) => {
  if (!agreeToTerms.value) {
    message.warning('请先同意用户协议和隐私政策')
    return
  }

  loading.value = true
  try {
    const res = await userRegister(values)
    // 注册成功，跳转到登录页面
    if (res.data.code === 0) {
      message.success('注册成功！欢迎加入零码智绘')
      await router.push({
        path: '/user/login',
        replace: true,
      })
    } else {
      message.error('注册失败：' + res.data.message)
    }
  } catch {
    message.error('注册失败，请稍后重试')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.register-page {
  width: 100%;
}

.register-header {
  text-align: center;
  margin-bottom: 32px;
}

.register-title {
  font-size: 28px;
  font-weight: 700;
  color: #262626;
  margin: 0 0 8px;
  background: linear-gradient(135deg, #1890ff, #722ed1);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.register-subtitle {
  font-size: 16px;
  color: #8c8c8c;
  margin: 0;
  font-weight: 400;
}

.register-form {
  margin-bottom: 24px;
}

.register-form :deep(.ant-form-item-label > label) {
  font-weight: 600;
  color: #262626;
}

.register-form :deep(.ant-form-item-required::before) {
  display: none !important;
}

.custom-input {
  border-radius: 8px;
  border: 2px solid #f0f0f0;
  transition: all 0.3s ease;
}

.custom-input:hover,
.custom-input:focus-within {
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.1);
}

.input-icon {
  color: #8c8c8c;
  font-size: 16px;
}

.password-strength {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 8px 0 16px;
  font-size: 14px;
}

.strength-text {
  color: #8c8c8c;
  white-space: nowrap;
}

.strength-bar {
  flex: 1;
  height: 4px;
  background: #f0f0f0;
  border-radius: 2px;
  overflow: hidden;
}

.strength-fill {
  height: 100%;
  border-radius: 2px;
  transition: all 0.3s ease;
}

.strength-fill.weak {
  background: #ff4d4f;
}

.strength-fill.medium {
  background: #faad14;
}

.strength-fill.strong {
  background: #52c41a;
}

.strength-label {
  font-weight: 600;
  white-space: nowrap;
}

.strength-label.weak {
  color: #ff4d4f;
}

.strength-label.medium {
  color: #faad14;
}

.strength-label.strong {
  color: #52c41a;
}

.register-button {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 8px;
  background: linear-gradient(135deg, #1890ff, #722ed1);
  border: none;
  box-shadow: 0 4px 12px rgba(24, 144, 255, 0.3);
  transition: all 0.3s ease;
}

.register-button:hover {
  background: linear-gradient(135deg, #40a9ff, #9254de);
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(24, 144, 255, 0.4);
}

.register-footer {
  text-align: center;
}

.login-tip {
  color: #8c8c8c;
  font-size: 14px;
  margin-bottom: 20px;
}

.login-link {
  color: #1890ff;
  font-weight: 600;
  text-decoration: none;
  transition: all 0.3s ease;
}

.login-link:hover {
  color: #40a9ff;
  text-decoration: underline;
}

.agreement {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 4px;
  font-size: 13px;
  color: #8c8c8c;
  line-height: 1.5;
}

.agreement-checkbox {
  margin-right: 8px;
}

.agreement-checkbox :deep(.ant-checkbox-inner) {
  border-radius: 4px;
}

.agreement-link {
  color: #1890ff;
  text-decoration: none;
  transition: all 0.3s ease;
}

.agreement-link:hover {
  color: #40a9ff;
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .register-title {
    font-size: 24px;
  }

  .register-subtitle {
    font-size: 14px;
  }

  .register-button {
    height: 44px;
    font-size: 15px;
  }

  .password-strength {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }

  .strength-text {
    text-align: center;
  }

  .agreement {
    flex-direction: column;
    gap: 8px;
  }
}
</style>
